package com.group2.operations.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group2.operations.common.api.ApiResult;
import com.group2.operations.mapper.ClientMapper;
import com.group2.operations.model.Client;
import com.group2.operations.model.vo.ClientInfoVO;
import com.group2.operations.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements ClientService{
    @Autowired
    private ClientMapper clientMapper;

    /**
     * 列出某厅店的所有客户
     * @param sid 厅店ID
     * @param pageNum 当前页数
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public PageInfo<Client> listClientsOfStore(int sid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Client> list = clientMapper.queryClientsBySid(sid);
        return new PageInfo<>(list);
    }

    /**
     * 为某个厅店添加用户
     * @param sid 厅店ID
     * @param vo 用户信息
     * @return
     */
    @Override
    public ApiResult addClient(int sid, ClientInfoVO vo) {
        try {
            clientMapper.addClient(sid, vo, new Date());
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ApiResult.failed("数据库内部错误");
        }

        return ApiResult.success("添加用户成功");
    }

    /**
     * 修改某个客户信息
     * @param cid 客户ID
     * @param vo 客户信息
     * @return
     */
    @Override
    public ApiResult updateClient(int cid, ClientInfoVO vo) {
        try {
            clientMapper.updateClient(cid, vo);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ApiResult.failed("数据库内部错误");
        }

        return ApiResult.success("客户信息更新成功");
    }

    /**
     * 删除某个客户
     * @param cid 客户ID
     * @return
     */
    @Override
    public ApiResult deleteClient(int cid) {
        try {
            clientMapper.deleteClient(cid);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ApiResult.failed("数据库内部错误");
        }

        return ApiResult.success("删除客户成功");
    }
}
